package bb.lanxing.adapter.discover;

import static android.os.Looper.getMainLooper;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.activity.discover.BbsDetailActivity;
import bb.lanxing.activity.team.LargeImageActivity;
import bb.lanxing.model.database.Bbs;

public class BbsListAdapter extends RecyclerView.Adapter<BbsListAdapter.ViewHolder> {
    private List<Bbs> list = new ArrayList<>();
    private OnItemClickListener listener;
    private Context mContext;

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout layout;
        ImageView head;
        TextView name;
        TextView content;

        RecyclerView imageGrid;
        BbsImageAdapter bbsImageAdapter;
        TextView time;
        TextView comment_number;

        LinearLayout likeLayout;
        ImageView likeIcon;
        TextView like_number;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = itemView.findViewById(R.id.layout);
            head = itemView.findViewById(R.id.head);
            name = itemView.findViewById(R.id.name);
            content = itemView.findViewById(R.id.content);
            imageGrid = itemView.findViewById(R.id.imageGrid);
            imageGrid.setLayoutManager(new GridLayoutManager(mContext, 3));
            bbsImageAdapter = new BbsImageAdapter();
            imageGrid.setAdapter(bbsImageAdapter);
            time = itemView.findViewById(R.id.time);
            comment_number = itemView.findViewById(R.id.comment_number);

            likeLayout = itemView.findViewById(R.id.bbs_like);
            likeIcon = itemView.findViewById(R.id.likeIcon);
            like_number = itemView.findViewById(R.id.like_number);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bbs_list, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (list != null) {
            Bbs bbs = list.get(position);
            if (bbs != null) {
                if (!"https://woc.bluebank.com.cn/prod-api".equals(bbs.userHead)) {
                    Glide.with(mContext).load(bbs.userHead).transform(new RoundedCorners(30)).into(holder.head);
                }
                holder.name.setText(bbs.createBy);
                holder.content.setText(bbs.content);
                if (bbs.imageList != null && bbs.imageList.size() > 0) {
                    holder.bbsImageAdapter.updateData(bbs.imageList);
                    holder.bbsImageAdapter.setOnItemClickListener(position1 -> {
                        Intent intent = new Intent(mContext, LargeImageActivity.class);
                        intent.putStringArrayListExtra("images", bbs.largeImageList);
                        intent.putExtra("currentImage", position1);
                        mContext.startActivity(intent);
                    });
                    holder.imageGrid.setVisibility(View.VISIBLE);
                } else {
                    holder.imageGrid.setVisibility(View.GONE);
                }

                holder.time.setText(bbs.createTime);
                holder.comment_number.setText(bbs.contentsCount + "");

                holder.likeLayout.setOnClickListener(v -> {
                    if (listener != null) {
                        listener.onItemClick(position);
                    }
                });
                holder.like_number.setText(bbs.likesCount + "");
                if (bbs.myLike == 1) {
                    holder.likeIcon.setImageResource(R.drawable.liked);
                    holder.like_number.setTextColor(mContext.getColor(R.color.bbs_liked));
                } else {
                    holder.likeIcon.setImageResource(R.drawable.like);
                    holder.like_number.setTextColor(mContext.getColor(R.color.bbs_like));
                }
                holder.layout.setOnClickListener(v -> {
                    Intent intent = new Intent(mContext, BbsDetailActivity.class);
                    intent.putExtra("userHead", bbs.userHead);
                    intent.putExtra("createBy", bbs.createBy);
                    intent.putExtra("content", bbs.content);
                    intent.putStringArrayListExtra("largeImageList", bbs.largeImageList);
                    intent.putStringArrayListExtra("imageList", bbs.imageList);
                    intent.putExtra("createTime", bbs.createTime);
                    intent.putExtra("contentsCount", bbs.contentsCount);
                    intent.putExtra("likesCount", bbs.likesCount);
                    intent.putExtra("myLike", bbs.myLike);
                    intent.putExtra("id", bbs.serverId);
                    intent.putExtra("bbsUserId", bbs.userId);
                    mContext.startActivity(intent);
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void updateData(List<Bbs> dataList) {
        list = dataList;
        new Handler(getMainLooper()).post(() -> notifyDataSetChanged());
    }
}